package server;

import handle.LocalClientCommandHandle;
import handle.LocalUpFileNotifyHandle;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import model.ShareFileModel;

import common.ClientModel;
import common.Constant;
import common.FileUpNotifyQueue;
import common.Server;

/**
 * Log 局域网服务.
 *
 * @author guanweichen
 * @version 2010-12-20
 */
public class LocalServer extends Server {
	
	public LocalServer() {
		initFileList(rb.getString(Constant.FILEKEY));
	}
	/**
	 * 启动服务.
	 * 
	 * @param port
	 */
	@Override
	public void startup(final Integer port) {
		
		if (!started) {
			started = true;
			this.port = port;
			Thread thread = new Thread(this);
			thread.start();
		} else {
			System.out.println("ERROR: Log server is started.........");
		}
	}
	
	/**
	 * 服务开始线程.
	 */
	@Override
	public void run () {

		try {
			System.out.println("Log server is started....................");
			serverSocket = new ServerSocket(port);
			while (started) {
				Socket socket =serverSocket.accept();
				// 命令处理线程--处理来自client的请求.
				LocalClientCommandHandle clientCommandHandle = new LocalClientCommandHandle(this, socket);
				clientCommandHandle.start();
				// 文件更新通知线程
				LocalUpFileNotifyHandle upFileNotifyHandle = new LocalUpFileNotifyHandle(this);
				upFileNotifyHandle.start();
			}
			serverSocket.close();
		} catch (IOException e) {
			System.out.println("ERROR: ServerSocket don't create............................");
			e.printStackTrace();
		}
	
	}
	/**
	 * 关闭服务.
	 * 
	 * @param port
	 */
	public void shutdown() {
		started = false;
	}
	
	protected void displayClients() {
		System.out.println("当前连接服务人数: " + listClient.size());
		for (ClientModel client : listClient) {
			System.out.println("IP: " + client.getAddress() );
			System.out.println("PORT: " + client.getPort() );
		}
	}
	
	public boolean addClient(ClientModel client) {
		
		for (ClientModel client2 : listClient) {
			if (client.getAddress().equals(client2.getAddress()) && client.getPort().equals(client2.getPort())) {
				return false;
			}
		}
		listClient.add(client);
		return true;
	}
	
	public boolean addShareFile(ShareFileModel shareFile) {
		
		if (shareFile.checkIsAdd()) {
			for (ShareFileModel file : listShareFile) {
				if (file.getFileName().equals(shareFile.getFileName())) {
					System.out.println("Warning: Add Share File to listShareFile failed. file's name is had.");
					System.out.println("Failed file's name is: " + shareFile.getServerFilePath());
					return false;
				}
			}
			listShareFile.add(shareFile);
		}
		return true;
	}
	
	private void initFileList(String files) {
		if (files != null && files != "") {
			String []fs = files.split(";");
			for (String f : fs) {
				String []file = f.split(",");
				ShareFileModel shareFile = new ShareFileModel(file[0], file[1],file[2]);
				addShareFile(shareFile);
			}
		}
	}
	@Override
	public boolean addFileUpNotify(FileUpNotifyQueue notifyQueue) {
		
		ClientModel client = notifyQueue.getClient();
		FileUpNotifyQueue removeNotifyQueue = null;
		for (FileUpNotifyQueue queue : listNotifyQueue) {
			if (queue.getClient().getAddress().equals(client.getAddress())
					&& queue.getClient().getPort().equals(client.getPort())){
				removeNotifyQueue = queue;
				if (queue.getShareFileModel().getFileName().equals(notifyQueue.getShareFileModel().getFileName())) {
					System.out.println("file's name: '" + notifyQueue.getShareFileModel().getFileName() + "' added.");
					return false;
				}
			}
				
		}
		listNotifyQueue.remove(removeNotifyQueue);// 删除过去监听
		listNotifyQueue.add(notifyQueue);// 增加现在监听
		return true;
	}
	@Override
	public boolean removeClient(ClientModel client) {
		for (ClientModel client2 : listClient) {
			if (client.getAddress().equals(client2.getAddress()) && client.getPort().equals(client2.getPort())) {
				listClient.remove(client2);
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean removeFileUpNotify(FileUpNotifyQueue notifyQueue) {
		ClientModel client = notifyQueue.getClient();
		for (FileUpNotifyQueue queue : listNotifyQueue) {
			if (queue.getClient().getAddress().equals(client.getAddress())
					&& queue.getClient().getPort().equals(client.getPort())
					&& queue.getShareFileModel().getFileName().equals(notifyQueue.getShareFileModel().getFileName()) ) {
				listNotifyQueue.remove(queue);// 删除监听
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean removeShareFile(ShareFileModel shareFile) {
		
		if (shareFile.checkIsAdd()) {
			for (ShareFileModel file : listShareFile) {
				if (file.getFileName().equals(shareFile.getFileName())) {
					listShareFile.remove(file);
					return true;
				}
			}
		}
		return false;
	}
	@Override
	public boolean removeFileUpNotifyByAddressAndPort(String address, Integer port) {
		for (FileUpNotifyQueue queue : listNotifyQueue) {
			if (queue.getClient().getAddress().equals(address) && queue.getClient().getPort().equals(port) ) {
				listNotifyQueue.remove(queue);// 删除监听
				return true;
			}
		}
		return false;
	}
}
